@tool
class_name State_Chart
extends EditorPlugin

enum SidebarLocation
{
	Left,
	Right,
}
#region 属性
# 2D侧边栏
var _ui_sidebar_canvas:Control
var _sidebar_ui:PackedScene = preload("res://addons/State_Chart/util/editor_sidebar/editor_sidebar.tscn")
var _debugger_plugin:EditorDebuggerPlugin
var _inspector_plugin:EditorInspectorPlugin
var _state_name_inspector_plugin:EditorInspectorPlugin
var _current_sidebar_location:SidebarLocation = SidebarLocation.Left
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
#endregion
#region 公共方法
#endregion
#region 私有方法
func _toggle_sidebar():
	_remove_sidebars()
	_current_sidebar_location = SidebarLocation.Right if _current_sidebar_location == SidebarLocation.Left else SidebarLocation.Left
	_add_sidebars()
	queue_save_layout()
func _add_sidebars():
	if _current_sidebar_location == SidebarLocation.Left:
		add_control_to_container(EditorPlugin.CONTAINER_CANVAS_EDITOR_SIDE_LEFT,_ui_sidebar_canvas)
	else:
		add_control_to_container(EditorPlugin.CONTAINER_CANVAS_EDITOR_SIDE_RIGHT,_ui_sidebar_canvas)
func _remove_sidebars():
	if _current_sidebar_location == SidebarLocation.Left:
		remove_control_from_container(EditorPlugin.CONTAINER_CANVAS_EDITOR_SIDE_LEFT,_ui_sidebar_canvas)
	else:
		remove_control_from_container(EditorPlugin.CONTAINER_CANVAS_EDITOR_SIDE_RIGHT,_ui_sidebar_canvas)
func _on_selection_changed():
	var selection = get_editor_interface().get_selection().get_selected_nodes()
	if selection.size() == 1:
		var selected_node = selection[0]
		if selected_node is StateChart or selected_node is State or selected_node is Transition:
			_ui_sidebar_canvas.show()
			_ui_sidebar_canvas.change_selected_node(selected_node)
			return
	_ui_sidebar_canvas.hide()
#endregion
#region 生命周期函数
func _enter_tree() -> void:
	_ui_sidebar_canvas = _sidebar_ui.instantiate()
	_ui_sidebar_canvas.sidebar_toggle_requested.connect(_toggle_sidebar)
	_ui_sidebar_canvas.hide()
	
	_add_sidebars()
	get_editor_interface().get_selection().selection_changed.connect(_on_selection_changed)
	
	_debugger_plugin = preload("res://addons/State_Chart/util/editor_debugger/editor_debugger_plugin.gd").new()
	_debugger_plugin.init(get_editor_interface().get_editor_settings())
	add_debugger_plugin(_debugger_plugin)
	
	_inspector_plugin = preload("res://addons/State_Chart/util/event_editor/event_inspector_plugin.gd").new()
	add_inspector_plugin(_inspector_plugin)
	
	_state_name_inspector_plugin = preload("res://addons/State_Chart/util/state_name_editor/StateNameListInspectorPlugin.gd").new()
	add_inspector_plugin(_state_name_inspector_plugin)
func _ready() -> void:
	_ui_sidebar_canvas.setup(get_editor_interface(),get_undo_redo())
	_inspector_plugin.setup(get_undo_redo())
	_state_name_inspector_plugin.setup(get_undo_redo())
func _exit_tree() -> void:
	remove_debugger_plugin(_debugger_plugin)
	remove_inspector_plugin(_inspector_plugin)
	remove_inspector_plugin(_state_name_inspector_plugin)
	_remove_sidebars()
	if is_instance_valid(_ui_sidebar_canvas):
		_ui_sidebar_canvas.queue_free()
func _set_window_layout(configuration: ConfigFile) -> void:
	_remove_sidebars()
	_current_sidebar_location = configuration.get_value("StateCharts","sidebar_location",SidebarLocation.Left)
	_add_sidebars()
func _get_window_layout(configuration: ConfigFile) -> void:
	configuration.set_value("StateCharts","sidebar_location",_current_sidebar_location)
#endregion
